import { scrollMixin } from "utils/mixin";
import extensionTabs from "./components/extensionTabs.vue";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      selected: "",

      // loadMore
      loading: false,
      goload: false,
      orderType: "",
      ratioDatas: [],
      api: "",
      withdrawal_type: "",

      tabs_list:[],
      list:[],

      //more
      isLoadMore: true,
      page: 1,
      total_page: 0
    };
  },
  created() {

    let selectedArr = ["", "0", "1", "2", "-1"];
    let selectedIndex = this.$route.query.selectIndex || 0;
    this.selected = selectedArr[selectedIndex];
    console.log(this.selected);

  },
  methods: {
    swichTabTItem() {
      console.log(this.selected);
      if (this.orderType === "supplier") {
        this.getSupplierNetData();
      } else if (this.orderType === "extension") {
        this.getData(this.selected);
      } else if (this.orderType == 'storeBalance'){
        this.getBalanceData(this.selected);
      } else {
        this.getNetData(this.selected);
      }
    },
    //发起http请求
    getNetData(status) {
      this.isLoadMore = false;
      this.page = 1;
      let json = {
        status: status, withdrawal_type: this.withdrawal_type 
      }
      $http
        .get(
          "finance.withdraw.withdraw-list",
          json,
          "正在获取列表"
        )
        .then(
          response => {
            if (response.result === 1) {
              this.isLoadMore = true;
              this.total_page = response.data.last_page;
              if (!this.total_page) {
                this.total_page = 0;
              }
              this.ratioDatas = [];
              //组装数据
              let itemDay = {};
              itemDay.money = response.data.today;
              itemDay.data = "今日";
              itemDay.name = "data";
              this.ratioDatas.push(itemDay);

              let itemYesterday = {};
              itemYesterday.money = response.data.yesterday;
              itemYesterday.data = "昨日";
              itemYesterday.name = "data";
              this.ratioDatas.push(itemYesterday);

              let itemWeek = {};
              itemWeek.money = response.data.lastweek;
              itemWeek.data = "本周";
              itemWeek.name = "data";
              this.ratioDatas.push(itemWeek);

              let itemMonth = {};
              itemMonth.money = response.data.thismonth;
              itemMonth.data = "本月累计提成";
              itemMonth.name = "mounth";
              this.ratioDatas.push(itemMonth);
              var mydata = response.data.data;
              this.list = mydata;
            } else {
              this.$toast(response.msg);
            }
          },
          function(response) {
            console.log(response);
          }
        );
    },
    getData(status) {
      this.isLoadMore = false;
      this.page = 1;
      $http
        .get(
          "finance.withdraw.withdraw-log",
          { status: status },
          "正在获取列表"
        )
        .then(
          response => {
            if (response.result === 1) {
              this.isLoadMore = true;
              this.total_page = response.data.last_page;
              if (!this.total_page) {
                this.total_page = 0;
              }
              let mydata = response.data.data;
              this.list = mydata;
            } else {
              this.$toast(response.msg);
            }
          },
          function(response) {
            console.log(response);
          }
        );
    },
    //供应商
    getSupplierNetData() {
      this.api = "plugin.supplier.frontend.withdraw-log.index";
      let json = {
        member_id: this.member_id?this.member_id:'',
      }
      if (this.selected.length == 0){
        json.status = '';
      } else if (this.selected == 0) {
        //待审核
        json.status = 1;
      } else if (this.selected == 1) {
        //待打款
        json.status = 2;
      } else if (this.selected == 4) {
        //打款中
        json.status = 4;
      } else if (this.selected == 2) {
        //已打款
        json.status = 3;
      } else if (this.selected == '-1') {
        //驳回/无效
        json.status = 5;
      } else if (this.selected == 3) {
        //已驳回
        json.status = '-2';
      }
      $http
        .get(
          this.api,
          json,
          "正在获取列表"
        )
        .then(
          response => {
            var mydata = response.data.data;
            this.total_page = response.data.last_page;
            this.isLoadMore = true;
            if (!this.total_page) {
              this.total_page = 0;
            }
            this.list = mydata;
          },
          function(response) {
            // error callback
          }
        );
    },
    //获取更多数据
    getMoreData() {
      let json = {
        status: this.selected
      };
      if (this.orderType === "supplier") {
        json.member_id=this.member_id;
      } else if (this.orderType === "extension") {
        this.api = "finance.withdraw.withdraw-log";
      } else if (this.orderType === "storeBalance") {
        this.api = "plugin.store-cashier.frontend.store.balance.award.store";
      } else {
        this.api = "finance.withdraw.withdraw-list";
        json.withdrawal_type=this.withdrawal_type
      }
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        json.page = this.page;
        $http
          .get(this.api,json, "加载中")
          .then(
            response => {
              this.isLoadMore = true;
              this.loading = false;
              if (response.result === 1) {
                var mydata = response.data.data;
                this.list = this.list.concat(this.orderType == 'storeBalance'?response.data.list.data:mydata);
              } else {
                this.page = this.page - 1;
                this.isLoadMore = false;
                this.loading = true;
              }
            },
            function(response) {
              // error callback
            }
          )
          .catch(err => {
            console.log(err);
          });
      }
    },
    initData() {
      this.page = 1;
      this.goload = true;
      this.total_page = 0;
      this.loading = true;
      this.isLoadMore = true;
      this.ratioDatas = [];
      
    },
    getBalanceData(ind){
      this.isLoadMore = false;
      this.page = 1;
      let json = { status: ind };
      $http
        .get(this.api, json, "")
        .then(
          response => {
            this.isLoadMore = true;
            this.loading = false;
            this.total_page = response.data.list.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
            if (response.result === 1) {
              var mydata = response.data.list.data;
              this.list = mydata;
              this.ratioDatas = [];
              //组装数据
              let itemDay = {};
              itemDay.money = response.data.statistic.today;
              itemDay.data = "今日";
              itemDay.name = "data";
              this.ratioDatas.push(itemDay);

              let itemYesterday = {};
              itemYesterday.money = response.data.statistic.yesterday;
              itemYesterday.data = "昨日";
              itemYesterday.name = "data";
              this.ratioDatas.push(itemYesterday);

              let itemWeek = {};
              itemWeek.money = response.data.statistic.week;
              itemWeek.data = "本周";
              itemWeek.name = "data";
              this.ratioDatas.push(itemWeek);

              let itemMonth = {};
              itemMonth.money = response.data.statistic.month;
              itemMonth.data = "本月累计提成";
              itemMonth.name = "mounth";
              this.ratioDatas.push(itemMonth);
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
              this.loading = true;
            }
          },
          function(response) {
            // error callback
          }
        )
        .catch(err => {
          console.log(err);
        });
    }
  },
  activated() {
    console.log("activated",this.$route.params);
    this.initData();
    this.withdrawal_type = this.$route.query.from;
    let tab_list = [{name:"",title:"全部"},{name:"0",title:"申请中"},{name:"1",title:"待打款"}]
    if (this.$route.params.supplier === "supplier"||this.$route.query.extension === "extension") {
      tab_list.push({name:"4",title:"打款中"},{name:"2",title:"已打款"})
      if(this.$route.params.supplier === "supplier"){
        tab_list.push({name:"-1",title:"驳回/无效"})
      }else{
        tab_list.push({name:"-1",title:"已失效"})
      }
    }else{
      tab_list.push({name:"2",title:"已打款"},{name:"-1",title:"已失效"});
    }
    tab_list.push({name:"3",title:"已驳回"});

    if (this.$route.params.supplier === "supplier") {
      this.orderType = "supplier";
      this.member_id = this.$route.params.member_id;
      this.fun.setWXTitle("我的" + this.fun.initWithdrawal());
      this.getSupplierNetData();
    } else if (this.$route.query.extension === "extension") {
      this.orderType = "extension";
      this.api = "finance.withdraw.withdraw-log";
      this.getData(this.selected);
    } else if(this.$route.query.from == 'storeBalance'){
      tab_list= [{name:"0",title:"全部"},{name:"1",title:"已结算"},{name:"2",title:"已分账"}]
      this.orderType = 'storeBalance';
      this.api = "plugin.store-cashier.frontend.store.balance.award.store";
      this.getBalanceData(this.selected);
      this.fun.setWXTitle('我的提成');
    }else if(this.$route.query.from == 'store_cashier'){
      tab_list= [{name:"3",title:"全部"},{name:"0",title:"未结算"},{name:"1",title:"已结算"},{name:"-1",title:"已失效"}];
      this.orderType = "store_cashier";
      this.selected = "3";
      this.getNetData(3);
    }else {
      tab_list= [{name:"3",title:"全部"},{name:"0",title:"未结算"},{name:"1",title:"已结算"},{name:"-1",title:"已失效"},{name:'2',title:'子商户'}];
      this.orderType = "store";
      this.selected = "3";
      this.getNetData(3);
    }
    this.tabs_list = tab_list;

  },
  components: { extensionTabs }
};
